package com.eastfair.common.service.impl;

import cn.hutool.core.date.StopWatch;
import com.eastfair.common.dto.GmApiCstAdrInfDtos;
import com.eastfair.common.dto.GmApiCstCtcInfDtos;
import com.eastfair.common.dto.GmApiCstIssubillInfDtos;
import com.eastfair.common.dto.GmApiCstNmInfDtos;
import com.eastfair.common.res.CrmResult;
import com.eastfair.common.service.CommonInterfaceCallRecordService;
import com.eastfair.common.service.CrmCustomerInfoSyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Crm 同步客户 Service.
 *
 * @author dqq
 * @date 2023 /1/31
 */
@Service
@Slf4j
public class CrmCustomerInfoSyncServiceImpl implements CrmCustomerInfoSyncService {

    @Value("${roma.ipport}")
    private String romaIpport;

    @Value("${roma.CRM-X-HW-ID}")
    private String romaXHWID;

    @Value("${roma.CRM-X-HW-APPKEY}")
    private String romaXHWAPPKEY;

    @Value("${crm.customer.url}")
    private String crmCustomerUrl;

    @Value("${crm.customer_address.url}")
    private String crmCustomerAddressUrl;

    @Value("${crm.customer_invoice.url}")
    private String crmCustomerInvoiceUrl;

    @Value("${crm.customer_contact.url}")
    private String crmCustomerContactUrl;

    @Resource
    private CommonInterfaceCallRecordService commonInterfaceCallRecordService;

    @Override
    public CrmResult syncCustomerInfo(GmApiCstNmInfDtos gmApiCstNmInfDtos) {
        log.info("syncCustomerInfo - 同步客户名称, gmApiCstNmInfDtos={}", gmApiCstNmInfDtos);
        String httpUrl = romaIpport + crmCustomerUrl;
        StopWatch sw = new StopWatch();
        sw.start("syncCustomerInfo");
        String result = commonInterfaceCallRecordService.processHttp(httpUrl, gmApiCstNmInfDtos, romaXHWID, romaXHWAPPKEY);
        log.info("syncCustomerInfo - 同步客户名称结果, result={}", result);
        sw.stop();
        log.info(sw.getLastTaskName(), sw.getTotalTimeMillis());
        return null;
    }

    @Override
    public CrmResult syncCustomerAddressInfo(GmApiCstAdrInfDtos gmApiCstAdrInfDtos) {
        log.info("syncCustomerAddressInfo - 同步客户地址, gmApiCstAdrInfDtos={}", gmApiCstAdrInfDtos);
        String httpUrl = romaIpport + crmCustomerAddressUrl;
        StopWatch sw = new StopWatch();
        sw.start("syncCustomerAddressInfo");
        String result = commonInterfaceCallRecordService.processHttp(httpUrl, gmApiCstAdrInfDtos, romaXHWID, romaXHWAPPKEY);
        log.info("syncCustomerAddressInfo - 同步客户地址结果, result={}", result);
        sw.stop();
        log.info(sw.getLastTaskName(), sw.getTotalTimeMillis());
        return null;
    }

    @Override
    public CrmResult syncCustomerInvoiceInfo(GmApiCstIssubillInfDtos gmApiCstIssubillInfDtos) {
        log.info("syncCustomerInvoiceInfo - 同步客户开票信息, gmApiCstIssubillInfDtos={}", gmApiCstIssubillInfDtos);
        String httpUrl = romaIpport + crmCustomerInvoiceUrl;
        StopWatch sw = new StopWatch();
        sw.start("syncCustomerInvoiceInfo");
        String result = commonInterfaceCallRecordService.processHttp(httpUrl, gmApiCstIssubillInfDtos, romaXHWID, romaXHWAPPKEY);
        log.info("syncCustomerInvoiceInfo - 同步客户开票信息结果, result={}", result);
        sw.stop();
        log.info(sw.getLastTaskName(), sw.getTotalTimeMillis());
        return null;
    }

    @Override
    public CrmResult syncCustomerContractInfo(GmApiCstCtcInfDtos gmApiCstCtcInfDtos) {
        log.info("syncCustomerContractInfo - 同步客户联系信息, gmApiCstCtcInfDtos={}", gmApiCstCtcInfDtos);
        String httpUrl = romaIpport + crmCustomerContactUrl;
        StopWatch sw = new StopWatch();
        sw.start("syncCustomerContractInfo");
        String result = commonInterfaceCallRecordService.processHttp(httpUrl, gmApiCstCtcInfDtos, romaXHWID, romaXHWAPPKEY);
        log.info("syncCustomerContractInfo - 同步客户联系信息结果, result={}", result);
        sw.stop();
        log.info(sw.getLastTaskName(), sw.getTotalTimeMillis());
        return null;
    }
}